

new Vue({
    data:{
        caseCategory:[],
        loading:false,
        caseCategoryId : 2, //合作案例的id默认为2
        currentCategoryId:2, //当前分类id
        articles:[],
        paginate:{},
        currentPage: 1,
        titleMap:{
            '满天星验光配镜智能一体机': 'All-Star Optometric Combined Glasses Prescription Intelligent Machine',
            '满天星微商城':' All-Star Wemall',
            '满天星开店宝PAD': 'All-Star Shopping Helper',
            '代理加盟': 'Distributors',
            '其他': 'Others',
            '满天星眼镜自动售卖机 ': 'All-Star Spectacles Vending Machine',
            '满天星试戴购买一体机': 'All-Star Eyewear Try-On Experience Machine'
        },
        lang: document.getElementsByTagName('html')[0].lang
    },

    methods:{

        // 翻译分类名称
        tans: function ( value ) {
            if( this.lang === 'zh' ) return value;
            return this.titleMap[value];
        },

        // 获取文章标题
        getArticleTitle: function (article) {
              if (this.lang === 'zh') {
                    return article.title;
              }else{
                  if( article.english_title && article.english_title.length > 95 ){
                      return article.english_title.substring(0,95) + '...';
                  }else{
                      return article.english_title;
                  }
              }
        },

        //根据默认id请求分类列表
        getCaseCategory:function(){
            this.loading = true;
            var self = this;
            var response =  self.$http.get('/api/web-page/news-article/create/').then(function(response){
                return response.json();
            }).catch(function(error){
                console.error(error);
            });

            response.then(function(result){
                if(result.code !== 0){
                    throw new Error("请求分类列表失败，code:"+result.code);
                }

                self.caseCategory = _(result.tags).find(function (el) {
                    return el.id === self.caseCategoryId
                })['sub_tag'];

               // self.currentCategoryId = self.caseCategory[0].id; //第一项为当前默认
                self.loading = false;

                //请求新闻摘要列表数据
                self.getCaseList();
            });
        },

        //请求案例摘要列表
        getCaseList:function(){
            var self = this;
            self.loading = true;
            this.$http.get("/api/web-page/news-article?page="+self.currentPage+"&per_page=9&tag_id="+self.currentCategoryId).then(function(response){
                return response.json();
            }).then(function(result){

                if(result.code !== 0){
                    throw new Error('请求案例摘要列表失败，code: '+ result.code);
                }
                self.articles = result['articles_paginate'].data;
                delete result['articles_paginate'].data;
                self.paginate = result['articles_paginate'];
                self.loading = false;

            }).catch(function(error){
                console.error(error);
            })
        },

        //切换分页
        changePage:function(number){
            this.currentPage = Number(number);
            this.getCaseList();
        },

        //切换分类
        changeCategory:function( categoryId ){
            categoryId = Number(categoryId);
            if( categoryId === this.currentCategoryId ){
                return;
            }else{
                this.currentPage = 1;
                this.currentCategoryId = categoryId;
            }
            this.getCaseList();
        }
    },

    components:{
        bymPagination: bymPagination
    },

    mounted:function(){
        //请求案例数据
        this.getCaseCategory();
    }

}).$mount('#case');
